//
//  PlanTableViewController.m
//  ICup
//
//  Created by liao Echo on 14-10-12.
//  Copyright (c) 2014年 Echo. All rights reserved.
//

#import "PlanTableViewController.h"
#import "singlePeripheral.h"

@interface PlanTableViewController ()

@end

@implementation PlanTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == 2) {
        return 44;
    }else{
        return 80;
    }
}
-(CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  3;
}

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"SettingBaseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.backgroundColor = [UIColor clearColor];//ios7必须要这样处理，不然默认会是白色
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"DPAT计划";
        UISwitch *switchBtn = [[UISwitch alloc] init];
        cell.accessoryView = switchBtn;
        if ([[NSUserDefaults standardUserDefaults] boolForKey:ENABLE_VIBRATE_KEY]) {
            [switchBtn setOn:YES];
        }else{
            [switchBtn setOn:NO];
        }
        isOpen = switchBtn.on;
        [switchBtn addTarget:self action:@selector(onSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];
    }else if(indexPath.row == 1){
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.text = @"每个人每天所需饮水量根据个人身体有很大的区别，系统根据您的个人信息为您推荐每日合理的饮水量。";
    }else if(indexPath.row == 2){
        cell.textLabel.text = @"日常计划";
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, 44)];
        label.textAlignment = UITextAlignmentRight;
        label.textColor = [UIColor blackColor];
        
        label.font = [UIFont systemFontOfSize:18];
        label.backgroundColor = [UIColor clearColor];
        [cell addSubview:label];
        
        label.text = [NSString stringWithFormat:@"%dml", [singlePeripheral getWaiterNum]];
        
        if (num > 0 && isOpen) {
            label.text = [NSString stringWithFormat:@"%dml", num];
            [UD setObject:[NSNumber numberWithInt:num] forKey:EVERYDAY_WAITER_NUM];
        }else{
            [UD setObject:[NSNumber numberWithInt:num] forKey:EVERYDAY_WAITER_NUM];
        }
    }
    return cell;
}
-(IBAction)onSwitchValueChanged:(UISwitch*)sender{
    [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:ENABLE_VIBRATE_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    isOpen = sender.on;
    
    if (!isOpen) {
        [tableView reloadData];
    }
    
}
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [atableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row ==2 ) {
        if (isOpen) {
            [self initPicker];
        }
    }
}
-(void)initPicker{
    btnbg = [[UIButton alloc] initWithFrame:self.view.bounds];
    btnbg.backgroundColor = RGB(0, 0, 0, .4);
    [self.view addSubview:btnbg];
    
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, FH(btnbg)-150, 0, 150)];
    if (IOS7_OR_LATER) {
        picker.backgroundColor = [UIColor grayColor];
    }
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [btnbg addSubview:picker];
    
    CGFloat bgviewY= FY(picker)-44;
    bgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, bgviewY, SCREEN_WIDTH, 44)];
    bgview.userInteractionEnabled = YES;
    bgview.image = [UIImage imageNamed:@"popping_bg.png"];
    
    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(266, 5, 53, 33)];
    [btn setImage:IMG(@"打钩 符号.png") forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(pickStatureDidFinish) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"popping_button.png"] forState:UIControlStateNormal];
    [bgview addSubview:btn];
    
    btn = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 53, 33)];
    [btn setImage:IMG(@"X 符号.png") forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(pickStatureDidCancel) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"popping_button.png"] forState:UIControlStateNormal];
    [bgview addSubview:btn];
    
    [btnbg addSubview:bgview];
}
-(void)pickStatureDidCancel{
    num = 0;
    [btnbg removeFromSuperview]; btnbg = nil;
}
-(void)pickStatureDidFinish{
    
    [btnbg removeFromSuperview]; btnbg = nil;
    
    [tableView reloadData];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 41;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

#pragma UIPickerView Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)componen{
    return [NSString stringWithFormat:@"%d", 1000+row*100];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    num = 1000+row*100;
}
@end
